import os
import requests
import re
import base64
import urllib3
import json
global LOL_project
global ChampionID
global Champion_name
global Champion_debuff
global Champion_debuff
#与LOL客户端交互类
class LOL:
    def __init__(self):
        urllib3.disable_warnings()
        # 运行 cmd命令 并进行获取输出结果
        result = os.popen('wmic PROCESS WHERE name="LeagueClientUx.exe" GET commandline')

        result = result.read().replace(' ', '').split(' ')

        # 获取 用户 token
        token = re.findall(re.compile(r'"--remoting-auth-token=(.*?)"'), result[0])

        # 获取 用户端口
        self.__port = re.findall(re.compile(r'"--app-port=(.*?)"'), result[0])

        # 或解密之后的 token
        self.__token = base64.b64encode(("riot:" + token[0]).encode("UTF-8")).decode("UTF-8")

        # 制作 请求头
        self.__headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": "Basic " + self.__token
        }
        # 构成 本机地址 端口
        self.__url = 'https://127.0.0.1:' + str(self.__port[0])
        # 获取championid和对应的英雄名称字典
        with open('champions.json', 'r', encoding='utf-8') as file:
             self.champions_id_name_json = json.load(file)
        self.champions_id_name_dict = {champion['id']: champion['name'] for champion in self.champions_id_name_json}
        #获取champion名称和对应debuff字典
        with open('champions_debuff.json', 'r', encoding='utf-8') as file:
             self.champions_debuff_json = json.load(file)
        self.champions_debuff_dict = {champion['name']: champion['debuff'] for champion in self.champions_debuff_json}

    def get(self, url):
        return requests.get(url=self.__url + url, headers=self.__headers, verify=False).json()

    def post(self, url):
        return requests.post(url=self.__url + url, headers=self.__headers, verify=False).json()
#窗口交互类
import tkinter as tk


class Application(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("版本:14.10 大乱斗实时获取英雄debuff")
        self.geometry("400x50")

        self.label = tk.Label(self, text="等待更新...")
        self.label.pack()

        # 初始化定时器以更新标签文本
        self.update_label_text()

    def update_label_text(self):
        # 更新a的值和标签文本
        print(LOL_project.get("/lol-champ-select/v1/session"))
        try :
            print(LOL_project.get("/lol-champ-select/v1/session/my-selection"))
            ChampionID = LOL_project.get("/lol-champ-select/v1/session/my-selection").get('championId')
            print(ChampionID)
            Champion_name = LOL_project.champions_id_name_dict.get(ChampionID)
            Champion_debuff = LOL_project.champions_debuff_dict.get(Champion_name)
            self.content = Champion_debuff
        except Exception as e:
            # 捕获其他类型的异常
            print(f"发生了一个错误: {e}")
            self.content = "未启动游戏或者选择英雄"
        finally:
            self.label.config(text=f"{Champion_name}: {Champion_debuff}")
        # 安排下一次更新，1000毫秒后
        self.after(1000, self.update_label_text)

    # 创建应用实例并启动Tkinter事件循环

if __name__ == '__main__':
    # 创建 LOL 类 实例对象
    LOL_project = LOL()
    app = Application()
    app.mainloop()

    #print(f"{Champion_name}:{Champion_debuff}")
    # 获取 召唤师名称
    #print(f'召唤师名称 : {(lOL_project.get("/lol-summoner/v1/current-summoner"))["displayName"]}')

    # 获取 召唤师 用户编号
    #print(f'用户编号 : {lOL_project.get("/lol-summoner/v1/current-summoner")["accountId"]}')
